from django.shortcuts import render
from .knn import KNN
from .naive_bayes import NaiveBayes
from .decision_tree import DecisionTree
from .svm import SVM
from .mlp import MLP
import re
from BigDataWeb.view import get_algorithm
from BigDataWeb.view import put_algorithm
from BigDataWeb.view import read_data_source


def knn(request):
    context = {}
    algorithm = KNN()
    algorithm.user_id = request.session["login_name"]
    put_algorithm(algorithm)
    context["algorithm"] = algorithm
    return render(request, "classifier/1.html", context)


def naive_bayes(request):
    context = {}
    algorithm = NaiveBayes()
    algorithm.user_id = request.session["login_name"]
    put_algorithm(algorithm)
    context["algorithm"] = algorithm
    return render(request, "classifier/1.html", context)


def decision_tree(request):
    context = {}
    algorithm = DecisionTree()
    algorithm.user_id = request.session["login_name"]
    put_algorithm(algorithm)
    context["algorithm"] = algorithm
    return render(request, "classifier/1.html", context)


def svm(request):
    context = {}
    algorithm = SVM()
    algorithm.user_id = request.session["login_name"]
    put_algorithm(algorithm)
    context["algorithm"] = algorithm
    return render(request, "classifier/1.html", context)


def mlp(request):
    context = {}
    algorithm = MLP()
    algorithm.user_id = request.session["login_name"]
    put_algorithm(algorithm)
    context["algorithm"] = algorithm
    return render(request, "classifier/1.html", context)


def select_data_source(request):
    context = {}
    algorithm = get_algorithm(request)
    read_data_source(request, algorithm)
    context["algorithm"] = algorithm
    return render(request, "classifier/2.html", context)


def understand_data_set(request):
    context = {}
    algorithm = get_algorithm(request)
    algorithm.setInPutFieldName(request.POST.getlist("input_field_names"))
    algorithm.setOutPutFieldName(request.POST.get("output_field_name"))
    context["algorithm"] = algorithm
    return render(request, "classifier/3.html", context)


def configure_parameters(request):
    context = {}
    algorithm = get_algorithm(request)
    if algorithm.algorithm_name == "KNN近邻法":
        algorithm.k_value = int(request.POST.get("k_value"))
        
    if algorithm.algorithm_name == "朴素贝叶斯":
        algorithm.model_type = request.POST.get("model_type")
        
    if algorithm.algorithm_name == "决策树":
        algorithm.criterion = request.POST.get("criterion")
        max_depth = request.POST.get("max_depth")
        if max_depth:
            algorithm.max_depth = int(max_depth)
        else:
            algorithm.max_depth = None
            
    if algorithm.algorithm_name == "支持向量机":
        algorithm.kernel = request.POST.get("kernel")
        
    if algorithm.algorithm_name == "多层神经网络":
        algorithm.solver = request.POST.get("solver")
        
    algorithm.train_size = float(request.POST.get("train_size")) * 0.01
    algorithm.test_size = float(request.POST.get("test_size")) * 0.01
    context["algorithm"] = algorithm
    return render(request, "classifier/4.html", context)


def upload_predict_input_values(request):
    context = {}
    algorithm = get_algorithm(request)
    algorithm.predict_input_values = []
    for line in request.POST.get("predict_input_values").split("\n"):
        line = line.strip()
        if line != "":
            _index = 0
            _array = []
            for x in re.split(",|，", line):
                column_name = algorithm.input_field_names[_index]
                _index = _index + 1
                dtype_kind = algorithm.data_source[column_name].dtype.kind
                if dtype_kind == "f":
                    _array.append(float(x))
                elif dtype_kind == "i":
                    _array.append(int(float(x)))
                else:
                    _array.append(x)
            algorithm.predict_input_values.append(_array)
    algorithm.implent()
    algorithm.generateNotebookFile()
    
    context["predict_column_names"] = algorithm.input_field_names + [algorithm.output_field_name] + ["可能性(%)"]
    context["predict_values"] = []
    for i in range(len(algorithm.predict_input_values)):
        context["predict_values"].append(algorithm.predict_input_values[i] + [algorithm.predict_output_values[i]] + [round(algorithm.predict_output_probas[i][0] * 100, 2)])
        
    context["algorithm"] = algorithm
    return render(request, "classifier/5.html", context)

